#!/usr/bin/python # for editor

import os.path
import sys

TARGET_BUILD    = 'compiler'
TARGET_INSTALL  = 'compiler-install'

LINEGUIDE       = './compiler/lineguide'

PARSE_OPTS_SRC  = 'parse_opts.lg'
CORE_SRCS       = 'command.c parse_opts.c parse.y scan.l node.c class.c scope.c name.c patch.c type.c context.c method_sig.c'
SOBJC4GCC_SRCS  = 'sobjc4gcc.c'

LIB_SOBJC_COMMONS   = 'sobjc-commons'
LIB_CORE            = 'core'
LIB_GC              = 'gc'
SOBJC               = 'sobjc'
SOBJC4GCC           = 'sobjc4gcc'
SOBJC_GCC           = 'sobjc-gcc'
COMMANDS            = [SOBJC, SOBJC_GCC]

Import('env bin_dir')
env = env.Clone()
env['LINEGUIDE'] = LINEGUIDE
for cmd in COMMANDS:
    env.Command(os.path.join(bin_dir, cmd), cmd,
               [Copy('$TARGET', '$SOURCE'), Chmod('$TARGET', 0755)])
env.Alias(TARGET_INSTALL, bin_dir)

def build(env):
    if env.is_targeted(TARGET_BUILD, TARGET_INSTALL):
        build_core_objs(env)
        build_parse_opts_obj(env)
        build_cmds(env)

def build_core_objs(env):
    for obj in Split(CORE_SRCS):
        env.Object(obj)

def build_parse_opts_obj(env):
    env.Lineguide(PARSE_OPTS_SRC)

def build_cmds(env):
    lib_core = env.Library(LIB_CORE, Split(CORE_SRCS))
    sobjc4gcc = env.Program(target=SOBJC4GCC, source=Split(SOBJC4GCC_SRCS),
            LIBS=[LIB_SOBJC_COMMONS, LIB_CORE, LIB_GC])
    env.Install(bin_dir, sobjc4gcc)

if '-h' not in sys.argv:
  build(env)

